/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Transporte;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Estadoespera;
import Entidades.Modelo;
import Entidades.Marca;
import Entidades.Transportetransportista;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Cargaempaque;
import Entidades.Remito;
import Entidades.Recepcion;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class TransporteJpaController {

    public TransporteJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Transporte transporte) {
        if (transporte.getTransportetransportistaCollection() == null) {
            transporte.setTransportetransportistaCollection(new ArrayList<Transportetransportista>());
        }
        if (transporte.getCargaempaqueCollection() == null) {
            transporte.setCargaempaqueCollection(new ArrayList<Cargaempaque>());
        }
        if (transporte.getRemitoCollection() == null) {
            transporte.setRemitoCollection(new ArrayList<Remito>());
        }
        if (transporte.getRecepcionCollection() == null) {
            transporte.setRecepcionCollection(new ArrayList<Recepcion>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadoespera estadoespera = transporte.getEstadoespera();
            if (estadoespera != null) {
                estadoespera = em.getReference(estadoespera.getClass(), estadoespera.getIdEstadoEspera());
                transporte.setEstadoespera(estadoespera);
            }
            Modelo modelo = transporte.getModelo();
            if (modelo != null) {
                modelo = em.getReference(modelo.getClass(), modelo.getIdmodelo());
                transporte.setModelo(modelo);
            }
            Marca marca = transporte.getMarca();
            if (marca != null) {
                marca = em.getReference(marca.getClass(), marca.getIdmarca());
                transporte.setMarca(marca);
            }
            Collection<Transportetransportista> attachedTransportetransportistaCollection = new ArrayList<Transportetransportista>();
            for (Transportetransportista transportetransportistaCollectionTransportetransportistaToAttach : transporte.getTransportetransportistaCollection()) {
                transportetransportistaCollectionTransportetransportistaToAttach = em.getReference(transportetransportistaCollectionTransportetransportistaToAttach.getClass(), transportetransportistaCollectionTransportetransportistaToAttach.getIdtransportetransportista());
                attachedTransportetransportistaCollection.add(transportetransportistaCollectionTransportetransportistaToAttach);
            }
            transporte.setTransportetransportistaCollection(attachedTransportetransportistaCollection);
            Collection<Cargaempaque> attachedCargaempaqueCollection = new ArrayList<Cargaempaque>();
            for (Cargaempaque cargaempaqueCollectionCargaempaqueToAttach : transporte.getCargaempaqueCollection()) {
                cargaempaqueCollectionCargaempaqueToAttach = em.getReference(cargaempaqueCollectionCargaempaqueToAttach.getClass(), cargaempaqueCollectionCargaempaqueToAttach.getIdCargaEmpaque());
                attachedCargaempaqueCollection.add(cargaempaqueCollectionCargaempaqueToAttach);
            }
            transporte.setCargaempaqueCollection(attachedCargaempaqueCollection);
            Collection<Remito> attachedRemitoCollection = new ArrayList<Remito>();
            for (Remito remitoCollectionRemitoToAttach : transporte.getRemitoCollection()) {
                remitoCollectionRemitoToAttach = em.getReference(remitoCollectionRemitoToAttach.getClass(), remitoCollectionRemitoToAttach.getIdRemito());
                attachedRemitoCollection.add(remitoCollectionRemitoToAttach);
            }
            transporte.setRemitoCollection(attachedRemitoCollection);
            Collection<Recepcion> attachedRecepcionCollection = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionRecepcionToAttach : transporte.getRecepcionCollection()) {
                recepcionCollectionRecepcionToAttach = em.getReference(recepcionCollectionRecepcionToAttach.getClass(), recepcionCollectionRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollection.add(recepcionCollectionRecepcionToAttach);
            }
            transporte.setRecepcionCollection(attachedRecepcionCollection);
            em.persist(transporte);
            if (estadoespera != null) {
                estadoespera.getTransporteCollection().add(transporte);
                estadoespera = em.merge(estadoespera);
            }
            if (modelo != null) {
                modelo.getTransporteCollection().add(transporte);
                modelo = em.merge(modelo);
            }
            if (marca != null) {
                marca.getTransporteCollection().add(transporte);
                marca = em.merge(marca);
            }
            for (Transportetransportista transportetransportistaCollectionTransportetransportista : transporte.getTransportetransportistaCollection()) {
                Transporte oldTransporteOfTransportetransportistaCollectionTransportetransportista = transportetransportistaCollectionTransportetransportista.getTransporte();
                transportetransportistaCollectionTransportetransportista.setTransporte(transporte);
                transportetransportistaCollectionTransportetransportista = em.merge(transportetransportistaCollectionTransportetransportista);
                if (oldTransporteOfTransportetransportistaCollectionTransportetransportista != null) {
                    oldTransporteOfTransportetransportistaCollectionTransportetransportista.getTransportetransportistaCollection().remove(transportetransportistaCollectionTransportetransportista);
                    oldTransporteOfTransportetransportistaCollectionTransportetransportista = em.merge(oldTransporteOfTransportetransportistaCollectionTransportetransportista);
                }
            }
            for (Cargaempaque cargaempaqueCollectionCargaempaque : transporte.getCargaempaqueCollection()) {
                Transporte oldTransporteOfCargaempaqueCollectionCargaempaque = cargaempaqueCollectionCargaempaque.getTransporte();
                cargaempaqueCollectionCargaempaque.setTransporte(transporte);
                cargaempaqueCollectionCargaempaque = em.merge(cargaempaqueCollectionCargaempaque);
                if (oldTransporteOfCargaempaqueCollectionCargaempaque != null) {
                    oldTransporteOfCargaempaqueCollectionCargaempaque.getCargaempaqueCollection().remove(cargaempaqueCollectionCargaempaque);
                    oldTransporteOfCargaempaqueCollectionCargaempaque = em.merge(oldTransporteOfCargaempaqueCollectionCargaempaque);
                }
            }
            for (Remito remitoCollectionRemito : transporte.getRemitoCollection()) {
                Transporte oldTransporteOfRemitoCollectionRemito = remitoCollectionRemito.getTransporte();
                remitoCollectionRemito.setTransporte(transporte);
                remitoCollectionRemito = em.merge(remitoCollectionRemito);
                if (oldTransporteOfRemitoCollectionRemito != null) {
                    oldTransporteOfRemitoCollectionRemito.getRemitoCollection().remove(remitoCollectionRemito);
                    oldTransporteOfRemitoCollectionRemito = em.merge(oldTransporteOfRemitoCollectionRemito);
                }
            }
            for (Recepcion recepcionCollectionRecepcion : transporte.getRecepcionCollection()) {
                Transporte oldTransporteOfRecepcionCollectionRecepcion = recepcionCollectionRecepcion.getTransporte();
                recepcionCollectionRecepcion.setTransporte(transporte);
                recepcionCollectionRecepcion = em.merge(recepcionCollectionRecepcion);
                if (oldTransporteOfRecepcionCollectionRecepcion != null) {
                    oldTransporteOfRecepcionCollectionRecepcion.getRecepcionCollection().remove(recepcionCollectionRecepcion);
                    oldTransporteOfRecepcionCollectionRecepcion = em.merge(oldTransporteOfRecepcionCollectionRecepcion);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Transporte transporte) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporte persistentTransporte = em.find(Transporte.class, transporte.getIdTransporte());
            Estadoespera estadoesperaOld = persistentTransporte.getEstadoespera();
            Estadoespera estadoesperaNew = transporte.getEstadoespera();
            Modelo modeloOld = persistentTransporte.getModelo();
            Modelo modeloNew = transporte.getModelo();
            Marca marcaOld = persistentTransporte.getMarca();
            Marca marcaNew = transporte.getMarca();
            Collection<Transportetransportista> transportetransportistaCollectionOld = persistentTransporte.getTransportetransportistaCollection();
            Collection<Transportetransportista> transportetransportistaCollectionNew = transporte.getTransportetransportistaCollection();
            Collection<Cargaempaque> cargaempaqueCollectionOld = persistentTransporte.getCargaempaqueCollection();
            Collection<Cargaempaque> cargaempaqueCollectionNew = transporte.getCargaempaqueCollection();
            Collection<Remito> remitoCollectionOld = persistentTransporte.getRemitoCollection();
            Collection<Remito> remitoCollectionNew = transporte.getRemitoCollection();
            Collection<Recepcion> recepcionCollectionOld = persistentTransporte.getRecepcionCollection();
            Collection<Recepcion> recepcionCollectionNew = transporte.getRecepcionCollection();
            List<String> illegalOrphanMessages = null;
            for (Transportetransportista transportetransportistaCollectionOldTransportetransportista : transportetransportistaCollectionOld) {
                if (!transportetransportistaCollectionNew.contains(transportetransportistaCollectionOldTransportetransportista)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transportetransportista " + transportetransportistaCollectionOldTransportetransportista + " since its transporte field is not nullable.");
                }
            }
            for (Cargaempaque cargaempaqueCollectionOldCargaempaque : cargaempaqueCollectionOld) {
                if (!cargaempaqueCollectionNew.contains(cargaempaqueCollectionOldCargaempaque)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Cargaempaque " + cargaempaqueCollectionOldCargaempaque + " since its transporte field is not nullable.");
                }
            }
            for (Remito remitoCollectionOldRemito : remitoCollectionOld) {
                if (!remitoCollectionNew.contains(remitoCollectionOldRemito)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Remito " + remitoCollectionOldRemito + " since its transporte field is not nullable.");
                }
            }
            for (Recepcion recepcionCollectionOldRecepcion : recepcionCollectionOld) {
                if (!recepcionCollectionNew.contains(recepcionCollectionOldRecepcion)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recepcion " + recepcionCollectionOldRecepcion + " since its transporte field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (estadoesperaNew != null) {
                estadoesperaNew = em.getReference(estadoesperaNew.getClass(), estadoesperaNew.getIdEstadoEspera());
                transporte.setEstadoespera(estadoesperaNew);
            }
            if (modeloNew != null) {
                modeloNew = em.getReference(modeloNew.getClass(), modeloNew.getIdmodelo());
                transporte.setModelo(modeloNew);
            }
            if (marcaNew != null) {
                marcaNew = em.getReference(marcaNew.getClass(), marcaNew.getIdmarca());
                transporte.setMarca(marcaNew);
            }
            Collection<Transportetransportista> attachedTransportetransportistaCollectionNew = new ArrayList<Transportetransportista>();
            for (Transportetransportista transportetransportistaCollectionNewTransportetransportistaToAttach : transportetransportistaCollectionNew) {
                transportetransportistaCollectionNewTransportetransportistaToAttach = em.getReference(transportetransportistaCollectionNewTransportetransportistaToAttach.getClass(), transportetransportistaCollectionNewTransportetransportistaToAttach.getIdtransportetransportista());
                attachedTransportetransportistaCollectionNew.add(transportetransportistaCollectionNewTransportetransportistaToAttach);
            }
            transportetransportistaCollectionNew = attachedTransportetransportistaCollectionNew;
            transporte.setTransportetransportistaCollection(transportetransportistaCollectionNew);
            Collection<Cargaempaque> attachedCargaempaqueCollectionNew = new ArrayList<Cargaempaque>();
            for (Cargaempaque cargaempaqueCollectionNewCargaempaqueToAttach : cargaempaqueCollectionNew) {
                cargaempaqueCollectionNewCargaempaqueToAttach = em.getReference(cargaempaqueCollectionNewCargaempaqueToAttach.getClass(), cargaempaqueCollectionNewCargaempaqueToAttach.getIdCargaEmpaque());
                attachedCargaempaqueCollectionNew.add(cargaempaqueCollectionNewCargaempaqueToAttach);
            }
            cargaempaqueCollectionNew = attachedCargaempaqueCollectionNew;
            transporte.setCargaempaqueCollection(cargaempaqueCollectionNew);
            Collection<Remito> attachedRemitoCollectionNew = new ArrayList<Remito>();
            for (Remito remitoCollectionNewRemitoToAttach : remitoCollectionNew) {
                remitoCollectionNewRemitoToAttach = em.getReference(remitoCollectionNewRemitoToAttach.getClass(), remitoCollectionNewRemitoToAttach.getIdRemito());
                attachedRemitoCollectionNew.add(remitoCollectionNewRemitoToAttach);
            }
            remitoCollectionNew = attachedRemitoCollectionNew;
            transporte.setRemitoCollection(remitoCollectionNew);
            Collection<Recepcion> attachedRecepcionCollectionNew = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionNewRecepcionToAttach : recepcionCollectionNew) {
                recepcionCollectionNewRecepcionToAttach = em.getReference(recepcionCollectionNewRecepcionToAttach.getClass(), recepcionCollectionNewRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollectionNew.add(recepcionCollectionNewRecepcionToAttach);
            }
            recepcionCollectionNew = attachedRecepcionCollectionNew;
            transporte.setRecepcionCollection(recepcionCollectionNew);
            transporte = em.merge(transporte);
            if (estadoesperaOld != null && !estadoesperaOld.equals(estadoesperaNew)) {
                estadoesperaOld.getTransporteCollection().remove(transporte);
                estadoesperaOld = em.merge(estadoesperaOld);
            }
            if (estadoesperaNew != null && !estadoesperaNew.equals(estadoesperaOld)) {
                estadoesperaNew.getTransporteCollection().add(transporte);
                estadoesperaNew = em.merge(estadoesperaNew);
            }
            if (modeloOld != null && !modeloOld.equals(modeloNew)) {
                modeloOld.getTransporteCollection().remove(transporte);
                modeloOld = em.merge(modeloOld);
            }
            if (modeloNew != null && !modeloNew.equals(modeloOld)) {
                modeloNew.getTransporteCollection().add(transporte);
                modeloNew = em.merge(modeloNew);
            }
            if (marcaOld != null && !marcaOld.equals(marcaNew)) {
                marcaOld.getTransporteCollection().remove(transporte);
                marcaOld = em.merge(marcaOld);
            }
            if (marcaNew != null && !marcaNew.equals(marcaOld)) {
                marcaNew.getTransporteCollection().add(transporte);
                marcaNew = em.merge(marcaNew);
            }
            for (Transportetransportista transportetransportistaCollectionNewTransportetransportista : transportetransportistaCollectionNew) {
                if (!transportetransportistaCollectionOld.contains(transportetransportistaCollectionNewTransportetransportista)) {
                    Transporte oldTransporteOfTransportetransportistaCollectionNewTransportetransportista = transportetransportistaCollectionNewTransportetransportista.getTransporte();
                    transportetransportistaCollectionNewTransportetransportista.setTransporte(transporte);
                    transportetransportistaCollectionNewTransportetransportista = em.merge(transportetransportistaCollectionNewTransportetransportista);
                    if (oldTransporteOfTransportetransportistaCollectionNewTransportetransportista != null && !oldTransporteOfTransportetransportistaCollectionNewTransportetransportista.equals(transporte)) {
                        oldTransporteOfTransportetransportistaCollectionNewTransportetransportista.getTransportetransportistaCollection().remove(transportetransportistaCollectionNewTransportetransportista);
                        oldTransporteOfTransportetransportistaCollectionNewTransportetransportista = em.merge(oldTransporteOfTransportetransportistaCollectionNewTransportetransportista);
                    }
                }
            }
            for (Cargaempaque cargaempaqueCollectionNewCargaempaque : cargaempaqueCollectionNew) {
                if (!cargaempaqueCollectionOld.contains(cargaempaqueCollectionNewCargaempaque)) {
                    Transporte oldTransporteOfCargaempaqueCollectionNewCargaempaque = cargaempaqueCollectionNewCargaempaque.getTransporte();
                    cargaempaqueCollectionNewCargaempaque.setTransporte(transporte);
                    cargaempaqueCollectionNewCargaempaque = em.merge(cargaempaqueCollectionNewCargaempaque);
                    if (oldTransporteOfCargaempaqueCollectionNewCargaempaque != null && !oldTransporteOfCargaempaqueCollectionNewCargaempaque.equals(transporte)) {
                        oldTransporteOfCargaempaqueCollectionNewCargaempaque.getCargaempaqueCollection().remove(cargaempaqueCollectionNewCargaempaque);
                        oldTransporteOfCargaempaqueCollectionNewCargaempaque = em.merge(oldTransporteOfCargaempaqueCollectionNewCargaempaque);
                    }
                }
            }
            for (Remito remitoCollectionNewRemito : remitoCollectionNew) {
                if (!remitoCollectionOld.contains(remitoCollectionNewRemito)) {
                    Transporte oldTransporteOfRemitoCollectionNewRemito = remitoCollectionNewRemito.getTransporte();
                    remitoCollectionNewRemito.setTransporte(transporte);
                    remitoCollectionNewRemito = em.merge(remitoCollectionNewRemito);
                    if (oldTransporteOfRemitoCollectionNewRemito != null && !oldTransporteOfRemitoCollectionNewRemito.equals(transporte)) {
                        oldTransporteOfRemitoCollectionNewRemito.getRemitoCollection().remove(remitoCollectionNewRemito);
                        oldTransporteOfRemitoCollectionNewRemito = em.merge(oldTransporteOfRemitoCollectionNewRemito);
                    }
                }
            }
            for (Recepcion recepcionCollectionNewRecepcion : recepcionCollectionNew) {
                if (!recepcionCollectionOld.contains(recepcionCollectionNewRecepcion)) {
                    Transporte oldTransporteOfRecepcionCollectionNewRecepcion = recepcionCollectionNewRecepcion.getTransporte();
                    recepcionCollectionNewRecepcion.setTransporte(transporte);
                    recepcionCollectionNewRecepcion = em.merge(recepcionCollectionNewRecepcion);
                    if (oldTransporteOfRecepcionCollectionNewRecepcion != null && !oldTransporteOfRecepcionCollectionNewRecepcion.equals(transporte)) {
                        oldTransporteOfRecepcionCollectionNewRecepcion.getRecepcionCollection().remove(recepcionCollectionNewRecepcion);
                        oldTransporteOfRecepcionCollectionNewRecepcion = em.merge(oldTransporteOfRecepcionCollectionNewRecepcion);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transporte.getIdTransporte();
                if (findTransporte(id) == null) {
                    throw new NonexistentEntityException("The transporte with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporte transporte;
            try {
                transporte = em.getReference(Transporte.class, id);
                transporte.getIdTransporte();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transporte with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Transportetransportista> transportetransportistaCollectionOrphanCheck = transporte.getTransportetransportistaCollection();
            for (Transportetransportista transportetransportistaCollectionOrphanCheckTransportetransportista : transportetransportistaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Transportetransportista " + transportetransportistaCollectionOrphanCheckTransportetransportista + " in its transportetransportistaCollection field has a non-nullable transporte field.");
            }
            Collection<Cargaempaque> cargaempaqueCollectionOrphanCheck = transporte.getCargaempaqueCollection();
            for (Cargaempaque cargaempaqueCollectionOrphanCheckCargaempaque : cargaempaqueCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Cargaempaque " + cargaempaqueCollectionOrphanCheckCargaempaque + " in its cargaempaqueCollection field has a non-nullable transporte field.");
            }
            Collection<Remito> remitoCollectionOrphanCheck = transporte.getRemitoCollection();
            for (Remito remitoCollectionOrphanCheckRemito : remitoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Remito " + remitoCollectionOrphanCheckRemito + " in its remitoCollection field has a non-nullable transporte field.");
            }
            Collection<Recepcion> recepcionCollectionOrphanCheck = transporte.getRecepcionCollection();
            for (Recepcion recepcionCollectionOrphanCheckRecepcion : recepcionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Recepcion " + recepcionCollectionOrphanCheckRecepcion + " in its recepcionCollection field has a non-nullable transporte field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Estadoespera estadoespera = transporte.getEstadoespera();
            if (estadoespera != null) {
                estadoespera.getTransporteCollection().remove(transporte);
                estadoespera = em.merge(estadoespera);
            }
            Modelo modelo = transporte.getModelo();
            if (modelo != null) {
                modelo.getTransporteCollection().remove(transporte);
                modelo = em.merge(modelo);
            }
            Marca marca = transporte.getMarca();
            if (marca != null) {
                marca.getTransporteCollection().remove(transporte);
                marca = em.merge(marca);
            }
            em.remove(transporte);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Transporte> findTransporteEntities() {
        return findTransporteEntities(true, -1, -1);
    }

    public List<Transporte> findTransporteEntities(int maxResults, int firstResult) {
        return findTransporteEntities(false, maxResults, firstResult);
    }

    private List<Transporte> findTransporteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Transporte as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Transporte findTransporte(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Transporte.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransporteCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Transporte as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
     public Transporte findTransporte_x_patente(String pat) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Transporte u where u.patenteTransporte = " + "'" + pat +"'");
            return (Transporte) q.getSingleResult();
        } finally {
            em.close();
        }
    }

}
